library(foreign)
library(reshape2)
library(doBy)

rm(list=ls(all=TRUE))

for (iteration in 1970:1999) {

setwd("C:\\Users\\rthavarajah\\Desktop\\Paper Stuff\\Bloom Master\\Spillovers Computation\\1 inputs\\patent fetch\\output")
set1 <- read.dta("subsidiaries_patents_byyear.dta")
set1 <- set1[which(set1$appyear==iteration), ]
keep1 <- as.vector(set1$cusip)
keep1 <- unique(keep1)

setwd("C:\\Users\\rthavarajah\\Desktop\\Paper Stuff\\Bloom Master\\Spillovers Computation\\1 inputs\\r and d data fetch")
set2 <- read.dta("r and d data_bloom.dta")
keep2 <- as.vector(set2$cusip)
keep2 <- unique(keep2)

setwd("C:\\Users\\rthavarajah\\Desktop\\Paper Stuff\\Bloom Master\\Spillovers Computation\\1 inputs\\ciq fetch\\output")
set3 <- read.dta("subsidiaries_revenue_bloom.dta")
keep3 <- as.vector(set3$cusip)  
keep3 <- unique(keep3)

keep1 <- keep1[keep1 %in% keep2]
keep1 <- keep1[keep1 %in% keep3]


########################SPILLTECH CORRELATIONS
setwd("C:\\Users\\rthavarajah\\Desktop\\Paper Stuff\\Bloom Master\\Spillovers Computation\\1 inputs\\patent fetch\\output")
statadata1 <- read.dta("subsidiaries_patents_byyear.dta")
statadata1 <- statadata1[statadata1$cusip %in% keep1, ]
statadata1 <- statadata1[ which(statadata1$appyear==iteration), ]
statadata1$appyear <- NULL
#format patent data
x1 <- as.matrix(statadata1)
firms <- x1[,1] 
x1 <- x1[,-1]
x1 <- apply(x1, 2, as.numeric) 
rownames(x1) <- firms

#convert counts to proportions
x1 <- x1/rowSums(x1) 

#compute numerators
num <- x1 %*% t(x1)

#compute denominators
#store diagonals
Tii_sqrt <- sqrt(diag(num))

#compute denominators
den <- Tii_sqrt %*% t(Tii_sqrt)
den <- den^-1

#compute tech spillovers correlation matrix
spilltech_corr <- num * den
rownames(spilltech_corr) <- rownames(x1)
colnames(spilltech_corr) <- rownames(x1)



#########################GET R&D DATA
setwd("C:\\Users\\rthavarajah\\Desktop\\Paper Stuff\\Bloom Master\\Spillovers Computation\\1 inputs\\r and d data fetch")
statadata2 <- read.dta("r and d data_bloom.dta")
statadata2 <- statadata2[statadata2$cusip %in% keep1, ]

#format r and d data
x2 <- as.matrix(statadata2)
firms2 <- x2[,1] 
x2 <- x2[,-1]
x2 <- apply(x2, 2, as.numeric) 
rownames(x2) <- firms2



########################SPILLSIC
setwd("C:\\Users\\rthavarajah\\Desktop\\Paper Stuff\\Bloom Master\\Spillovers Computation\\1 inputs\\ciq fetch\\output")
statadata3 <- read.dta("subsidiaries_revenue_bloom.dta")
statadata3 <- statadata3[statadata3$cusip %in% keep1, ]

#format revenue data
x3 <- as.matrix(statadata3)
firms3 <- x3[,1] 
x3 <- x3[,-1]
x3 <- apply(x3, 2, as.numeric) 
rownames(x3) <- firms3

#convert counts to proportions
x3 <- x3/rowSums(x3) 

#compute numerators
num <- x3 %*% t(x3)

#compute denominators
#store diagonals
Tii_sqrt <- sqrt(diag(num))

#compute denominators
den <- Tii_sqrt %*% t(Tii_sqrt)
den <- den^-1

#compute tech spillovers correlation matrix
spillsic_corr <- num * den
rownames(spillsic_corr) <- rownames(x3)
colnames(spillsic_corr) <- rownames(x3)






#compute spilltech
diag(spilltech_corr) <- 0
spilltech <- spilltech_corr %*% x2

#compute spillsic
diag(spillsic_corr) <- 0
spillsic <- spillsic_corr %*% x2

################SAVE OUTPUTS
spilltech <- data.frame(spilltech)
spillsic <-data.frame(spillsic)
spilltech$ciq <- rownames(spilltech)
spillsic$ciq <- rownames(spillsic)

setwd("C:\\Users\\rthavarajah\\Desktop\\Paper Stuff\\Bloom Master\\Spillovers Computation\\2 intermediate - R code Jaffe\\output")
write.dta(spilltech, file=paste("spilltec", iteration, ".dta", sep=""))
spilltech_corr <- data.frame(spilltech_corr)
write.dta(spilltech_corr, file=paste("spilltec_corr", iteration, ".dta", sep=""))
write.dta(spillsic, "spillsic.dta")

}
